package com.iyundao.io.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.GiftService;
import com.iyundao.io.mapper.GiftMapper;
import com.iyundao.io.model.Gift;
import com.iyundao.io.tools.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GiftServiceImpl implements GiftService {

    @Autowired
    private GiftMapper giftMapper;

    /**
     * 修改礼品状态
     * @param id
     *      礼品id
     * @return
     */
    @Override
    public ResponseData updateFlag(Integer id) {
        int i = giftMapper.updateFlag(id);
        if (i > 0) {
            return new ResponseData(200, "修改成功");
        } else {
            return new ResponseData(0,"修改失败");
        }
    }

    @Override
    public ResponseData insert(Gift gift) {
        int i = giftMapper.insert(gift);
        if (i > 0) {
            return new ResponseData(200, "添加成功");
        } else {
            return new ResponseData(0,"添加失败");
        }
    }

    /**
     * 根据组织id查询组织下所有的礼品
     * @param pagenum
     *      页数
     * @param pagesize
     *      每页显示条数
     * @param organizationid
     *      组织id
     * @return
     */
    @Override
    public PageInfo<Gift> selectByOrganizationId(Integer pagenum, Integer pagesize, String organizationid) {
        List<Gift> list = giftMapper.selectByOrganizationId(organizationid);
        PageHelper.startPage(pagenum,pagesize);
        PageInfo<Gift> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public ResponseData selectById(Integer id) {
        Gift gift = giftMapper.selectById(id);
        if (gift.equals("") || gift == null) {
            return new ResponseData(0, "未查询到数据");
        } else {
            return new ResponseData(200, "成功", gift);
        }
    }

    @Override
    public ResponseData updateGift(Gift gift) {
        int i = giftMapper.updateSelective(gift);
        if (i > 0) {
            return new ResponseData(200, "修改成功");
        } else {
            return new ResponseData(0, "修改失败");
        }
    }
}
